moddbfandomcom-20200213-history
PopCap:Tutorials:Breakout:Part6
Introduction Hello and welcome to part 6 of my PopCap breakout tutorial series. I would like to take this time to give a special thanks to Gabberkooij for transferring a lot of these tutorials to the wiki and for his enthusiasm. If you haven’t read the first five tutorials then they can be found here: Part 1 Part 2 Part 3 Part 4 Part 5 In the last part of the tutorial series, we focused on creating a brick manager. In this part of the series we will look at adding the Box 2d physics engine to our game from: http://www.box2d.org/ Physics what? There comes a time in the course of developing a game where it can be a great benefit to use libraries that others have written in order to accomplish various tasks. We want to improve the mechanics of our game, however doing this without a physics library would take a great deal of complex math to accomplish. The physics library will handle collision response and ball movement for us, and all we need to do is draw our objects at the positions that we retrieve from the library. Working with other people's code can be a bit daunting. Chances are the library uses a different coding style then you use yourself, and sometimes the documentation might not be as clear as you would like. However in return we get a library that is typically well tested and that has taken a great deal of time to develop. One of the first things that you need to consider when deciding on whether to use a library is the license. The license of the library dictates the terms under which you can use their library in your application. Some libraries will require that you provide your source code open source, some libraries might require that you do not sell your work. It is important to ensure that the terms granted to you in the license are appropriate for your situation and when in doubt it is best to ask the developer of the library directly for clarification on their terms. The license for the Box 2D license as per this writing fall under the zlib license. This means that the license falls under the following terms: http://en.wikipedia.org/wiki/Zlib_License In other words you can't claim you wrote their physics library, you can't make modifications to the library and claim it is the original physics library (an updated version), and you can't move the zlib notice from any source code distributions. Furthermore the license does not require source code to be available and there are no restrictions on selling your work. This library happens to have a very nonrestrictive license and so it makes a good choice to use in our game. A small interlude Before we begin introducing a physics engine into our breakout game, I wanted to take a bit of time to introduce some new graphics and the start of a theme. Feel free to use your own graphics or use your own theme, but in case you are completely unmotivated to make graphics I have done this part for you! I decided that it would be cool to make the breakout game that the levels allude to places that you would need to escape from. I wanted the paddle to be a convict and I wanted the ball to be some sort of improvised ice ball. The first stage that I had in mind for the game was a murder scene in a jail cell. We are not told exactly what happened and we don't quite see a body, but we know that something is not quite right. This provides an interesting place to escape from none the less! Other ideas for levels include escaping from a capsized boat, and escaping from an ice block in the antarctic. To create the graphics I decided to use a program called The Gimp: http://www.gimp.org/ . The Gimp is a free image manipulation program that is comparable to Adobe Photoshop in ability. I decided to not use Photoshop because not everyone has such a program and it is quite expensive. Using The Gimp is outside the scope of this tutorial (I am not an artist in any sense) but I wanted to provide a mention to a free graphics package. Changing the graphics We have some new graphics which you can save to the debug folder. They have the 6 attached to the name because this is part 6 of the tutorial. A new bat image With a alpha image (remove Alpha while saving) A new ball image A new brick image And finally a background To use those graphics we will need some changes in the source code. For the background in the game we will need a image object. First open GameApp.h and locate DDImage* mBrickImg; Add after that line DDImage* mBackgroundLevel1Img; And in the GameApp.cpp we will add the logic to read the graphics with the new names and image in the LoadingThreadProc method void GameApp::LoadingThreadProc() { mBallImg=(DDImage*)GetImage("Ball6"); mPaddleImg=(DDImage*)GetImage("Bat6"); mBrickImg=(DDImage*)GetImage("Brick6"); mBackgroundLevel1Img=(DDImage*)GetImage("Level1Back"); } In the destructor ~GameApp we will need to add to the end of the method. delete mBackgroundLevel1Img; After this the image will be loaded from the application folder and loaded in memory. Now we can use it in the Board.cpp and draw it at the background. Locate in the Draw method g->FillRect(0, 0, mWidth, mHeight); and add the line g->DrawImage(mApp->mBackgroundLevel1Img, 0,0); Now hit F5 and play the game with a nice background and images. Resource management Maybe we can add here something about resource managment? And GameApp is not the best location to load a level dependant image... Attached is the latest code for the project until now. Physics Using the Box2d library Building a world Bodies The brick body The ball body The paddle body Listening to collisions Responding to collisions What we have done in this tutorial In this tutorial we improved the graphics and tweaked the game mechanics. Until next time… Like always I hope you enjoyed this part of the tutorial. In the next installment we ...whatever we wanna go to do... Continue to part 7 (not yet started) Attached is the latest code for this project. Legal Stuff I take no responsibility what so ever for any damages or liabilities from the code and/or information presented in this tutorial. This tutorial is provided AS-IS with no warranty what so ever.